package com.school.edupoint.config;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.impl.AMQImpl;
import com.school.edupoint.model.StudentActivityHistory;
import com.school.edupoint.service.StudentActivityHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MessageConsumer {

    @Autowired
    private StudentActivityHistoryService studentActivityHistoryService;

    @RabbitListener(queues = "${rabbitmq.queue.name}")
    public void receiveMessage(Message message) {
        try {
            byte[] body = message.getBody();
            StudentActivityHistory history = JSON.parseObject(body, StudentActivityHistory.class);

            studentActivityHistoryService.save(history);
            log.info("数据保存成功:: " + history);
        } catch (Exception e) {
            System.err.println("保存失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}